package com.zz.common.tools.box.tools;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import com.zz.common.tools.box.BpBox;
import com.zz.common.tools.box.runner.RunnerInfo;
import com.zz.common.utils.FileUtil;
import com.zz.common.utils.ZLog;

public class IconBox extends BpBox{
	
	public IconBox() {
		super(null, null);
	}

	public interface OnLoadIconListener{
		public void onSuccess(String iconUrl, String localPath, IconBox box);
		public void onFailed(String iconUrl, int errCode, IconBox box);
	}
	
	private static final String TAG = "IconBox";
	private String mIconUrl;
	private String mLocalPath;
	private OnLoadIconListener mListener;
	private BufferedOutputStream mOutputStream;
	
	public int loadIcon(String iconUrl, String filePath, OnLoadIconListener listener) {
//		Util.log(TAG, "" + mIconUrl);
		mIconUrl = iconUrl;
		mListener = listener;
		mLocalPath = filePath;
		
		int taskId = 0;
		if(mIconUrl.equals(mLocalPath)) {
			BpFuture future = new BpFuture() {
				
				@Override
				public void run() {
					if(FileUtil.isFileExist(mLocalPath)) {
						mListener.onSuccess(mIconUrl, mLocalPath, IconBox.this);
						setStatus(RunnerInfo.STATUS_SUCCEED);
					} else {
						mListener.onFailed(mIconUrl, -1, IconBox.this);
						setStatus(RunnerInfo.STATUS_FAILED);
					}
				}
			};
			setBpFuture(future);
			taskId = runBox(this);
		} else {
		
			BpUrlLoader urlLoader = new BpUrlLoader(mIconUrl);
	//		urlLoader.setHttpMethod("GET", null);
			urlLoader.setHttpMethod("GET", null, null, null);
			
			urlLoader.setBpOnUrlLoaderCompleteListener(new BpUrlLoader.IBpOnUrlLoaderCompleteListener() {
				

				public void onComplete(int respCode, Map<String, List<String>> headers,
						byte[] content, BpUrlLoader loader) {
					ZLog.d(TAG, mIconUrl + "\n\t\tCompleted " + mListener);
					setStatus(RunnerInfo.STATUS_SUCCEED);
					if(null != mOutputStream) {
						try {
							mOutputStream.close();
							mOutputStream = null;
							File file = new File(mLocalPath + "~tmp");
							file.renameTo(new File(mLocalPath));
						} catch(Exception e) {
						}
					}
					
					if(null != mListener) {
//						if(HttpURLConnection.HTTP_OK == respCode && isValidFile(headers)) {
						if(HttpURLConnection.HTTP_OK == respCode) {
							mListener.onSuccess(mIconUrl, mLocalPath, IconBox.this);
						} else {
							mListener.onFailed(mIconUrl, respCode, IconBox.this);
						}
					}
					
				}
			});
			
			urlLoader.setBpOnUrlLoaderErrorListener(new BpUrlLoader.IBpOnUrlLoaderErrorListener() {
				
				@Override
				public void onError(int errCode, BpUrlLoader loader) {
					ZLog.d(TAG, mIconUrl + "\n\t\tFailed " + mListener);
					setStatus(RunnerInfo.STATUS_FAILED);
					if(null != mOutputStream) {
						try {
							mOutputStream.close();
							mOutputStream = null;
						} catch(Exception e) {
						}
					}
					
					if(null != mListener) {
						mListener.onFailed(mIconUrl, errCode, IconBox.this);
					}
				}
			});
			
			urlLoader.setBpOnUrlLoaderReadListener(new BpUrlLoader.IBpOnUrlLoaderReadListener() {
				
				@Override
				public void onRead(byte[] buffer, int readedBytes, BpUrlLoader loader) {
						try {
							if (mOutputStream == null) {
								mOutputStream = new BufferedOutputStream(
										new FileOutputStream(new File(mLocalPath + "~tmp")));
							}
							if (null != mOutputStream) {
								mOutputStream.write(buffer, 0, readedBytes);
							}
						} catch (IOException e) {
							e.printStackTrace();
							loader.cancel();
						}
				}
			});
			setBpFuture(urlLoader);
			taskId = runBox(this);
		}
		
		return taskId;		
	}
	
}
